/*
作者:龙卫国
更新时间:2008-09-10

注:rel只能设置字串,数字与布尔值参数
*/

(function($) {

	$.jqPanel = {
		zIndex : 300000,
		currId : 0,
		windows : {},
		groupAreas : [],
		groupModules : [],

		/* opt = {
			title : '',
			width :320,
			height : 240,
			min_width : 150,
			min_height : 100,
			max_width : 1024,
			max_height : 800,
			url : '',
			left : 0,
			top : 0,
			moveable : true,
			resizeable : true,
			closeable : true,
			type : '',
			statusbar : false,
			minimizable : false,
			maximizable : false,
			mask : false,
			iframe : false,
			ajaxOptions : {},
			content : '',
			callback : null,
			context:["id","bottom"], //对齐到元素ID的相应方位, bottom,right,top,left
			hide: false, //默认是否为隐藏
			refresh:0,   //自动刷新时间间隔
			inline:false //面板在行内显示,而不是浮在页面
		}*/
		open : function(opt){
			var t = this, id, cls = '', dw = 0, dh = 0, vp, po, mdf, clf, we, w, h_set=false, jqp_id, hd_html, bd_html, ft_html;
			var showhd = showft = showbutton = showcancel = showload = showicon = showrefresh = false;

			opt = opt || {};
			if (!opt.type) opt.type = 'panel';

			id  = opt.id ? opt.id : $.global.generateId();

			if (t.windows[id]) return t.show(id);

			if (opt.id && (jqp_id = $("#"+opt.id)).length > 0){
				hd_html = $(".hd", jqp_id).html();
				bd_html = $(".bd", jqp_id).html();
				ft_html = $(".ft", jqp_id).html();
			}
			hd_html = hd_html || opt.title;
			bd_html = bd_html || opt.content
			if (bd_html) opt.url = '';

			if (opt.url){
				var baseURL;
				if(opt.url.indexOf("?")!==-1) baseURL = opt.url.substr(0, opt.url.indexOf("?"));
				else baseURL = opt.url;

				var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
				var urlType = baseURL.toLowerCase().match(urlString);

				if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){
					opt.content = '<img src="'+opt.url+'" onload="jQuery.jqPanel.resetPlace(\''+id+'\')">';
					showload = true;
					opt.url = '';
				}
				else {
					if (document.domain) opt.url += (opt.url.indexOf('?') == -1 ? '?' : '&') + 'mce_rdomain=' + document.domain;
				}
			}

			w = t.windows[id] = {
				id : id,
				jqp_panel : null,
				jqp_wrapper : null,
				jqp_iframe : null,
				jqp_bd : null,
				jqp_ft : null,
				jqp_mask : null,
				options : opt,
				//deltaWidth : 0, //边界宽度
				//deltaHeight : 0,
				zIndex :0,
				content : '',
				hd_height: 0,
				ft_height: 0,
				autoId : opt.id ? false : true,
				is_hide : false,
				is_min : false,
				close : function(){t.close(id);},
				hide : function(){t.close(id);},
				show : function(){t.show(id);},
				refresh_able : false,
				interval : null
			};

			if (!opt.width && opt.inline)opt.width = "100%";
			if (opt.height)opt.height = parseInt(opt.height) + ($.browser.msie ? 8 : 0);
			opt.min_width = parseInt(opt.min_width || (opt.type == 'dropdown' || opt.type == 'tooltip' ? 80 : 320));
			opt.min_height = parseInt(opt.min_height || (opt.type == 'dropdown' || opt.type == 'tooltip' ? 20 : 60));
			opt.max_width = parseInt(opt.max_width || 800);
			opt.max_height = parseInt(opt.max_height || 600);
			if (opt.moveable != false)opt.moveable = true;
			if (opt.resizeable != false)opt.resizeable = true;
			if (opt.closeable != false) opt.closeable = true;
			if (opt.minimizable != false) opt.minimizable = true;
			if (opt.shadow != false)opt.shadow = true;
			opt.refresh = parseInt(opt.refresh || 0);

			cls += ' jqp_panel_' + opt.type;
			if (opt.inline)cls += ' jqp_inline';
			if (opt.type == 'alert' || opt.type == 'confirm' || opt.type == 'dropdown' || opt.type == 'tooltip'){
				opt.resizeable = false;
				opt.minimizable = false;
				if (opt.type == 'dropdown' || opt.type == 'tooltip'){
					opt.moveable = false;
					opt.closeable = false;
				}
			}
			if (opt.moveable)cls += ' jqp_hd_move';

			if (opt.type == 'alert' || opt.type == 'confirm' || opt.type == 'dialog') showbutton = true;
			if (opt.type == 'dialog' || opt.type == 'confirm') showcancel = true;
			if (opt.type == 'alert' || opt.type == 'confirm') showicon = true;
			if (opt.type != 'alert' && opt.type != 'confirm' && opt.url) {
				showload = true;
				w.refresh_able = showrefresh = true;
			}
			if (hd_html || opt.closeable || opt.moveable || opt.minimizable) showhd = true;
			if (ft_html || showbutton || opt.resizeable) showft = true;

			var str = '';
			if (opt.mask){
				str += '<div id="'+id+'_mask" class="jqp_mask"></div>';
			}
			str += '	<div id="'+id+'_panel" class="jqp_panel '+cls+'">';
			str += '		<div style="position:relative">';
			if (opt.shadow){
				str += '		<div id="'+id+'_underlay" class="jqp_underlay"></div>';
			}
			str += '			<div id="'+id+'" class="jqp_wrapper">';
			if (showhd) {
				str += '			<div id="'+id+'_hd" class="hd">'+(hd_html || "Panel")+'</div>';
			}
			str += '				<div id="'+id+'_bd" class="bd">'+(bd_html || '')+'</div>';
			if (showft){
				str += '			<div id="'+id+'_ft" class="ft"> '+(ft_html || '');
				if (showbutton) {
					str += '			<div id="'+id+'_button_group" class="jqp_button_group">';
					str += '				<button type="button" id="'+id+'_ok" class="jqp_submit">'+(opt.type == 'dialog' ? 'Submit' : 'Ok')+'</button>';
					if (showcancel){
						str += '			<button type="button" class="jqp_cancel">Cancel</button>';
					}
					str += '			</div>';
				}
				if (opt.resizeable) {
					str += '			<div id="'+id+'_resize" class="jqp_resize_br">&nbsp;</div>';
				}
				str +='				</div>';
			}
			if (opt.closeable){
				str += '			<div id="'+id+'_close" class="jqp_close">&nbsp;</div>';
			}
			if (opt.minimizable){
				str += '			<div id="'+id+'_minimiz" class="jqp_minimiz_open">&nbsp;</div>';
			}
			if (showrefresh){
				str += '			<div id="'+id+'_refresh" class="jqp_refresh">&nbsp;</div>';
			}
			if (showicon){
				str += '			<div class="jqp_icon"></div>';
			}
			if (showload){
				str += '			<div id="'+id+'_load" class="jqp_load">&nbsp;</div> ';
			}
			str += '			</div>';
			str += '		</div>';
			str += '	</div>';

			if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
				$("body","html").css({height: "100%", width: "100%"});
				$("html").css("overflow","hidden");
			}
			if (jqp_id && jqp_id.length > 0){
				jqp_id.after(str);
				jqp_id.remove();
			}
			else if (opt.inline && opt.el) {
				opt.el.after(str);
				if (opt.el.html() == '') opt.el.remove();
			}
			else $("body").append(str);

			w.jqp_panel = $("#"+id+"_panel");
			w.jqp_wrapper = $("#"+id);
			if (opt.mask)w.jqp_mask = $("#"+id + '_mask');
			w.jqp_bd = $("#"+id + '_bd');
			if (opt.minimizable)w.jqp_minimiz = $("#"+id + '_minimiz');
			if (opt.resizeable)w.jqp_resize = $("#"+id + '_resize');
			if (showrefresh)w.jqp_refresh = $("#"+id + '_refresh');

			if (opt.minimizable && !opt.closeable)w.jqp_minimiz.css("right", 6);
			if (showrefresh){
				if (!opt.closeable && !opt.minimizable)w.jqp_refresh.css("right", 6);
				else if (!opt.closeable || !opt.minimizable)w.jqp_refresh.css("right", 35);
			}

			w.hd_height = !showhd ? 0 : $("#"+id + '_hd').height();
			w.ft_height = !showft ? 0 : $("#"+id + '_ft').height();
			if (showhd)w.jqp_hd = $("#"+id + '_hd');
			if (showft)w.jqp_ft = $("#"+id + '_ft');

			if (opt.height) w.jqp_bd.css("height", opt.height - w.hd_height - w.ft_height);

			if (showicon) w.jqp_bd.html('<div>'+opt.content.replace('\n', '<br />')+'</div>');
			else if (opt.type == 'dialog' || opt.type == 'tooltip' || !opt.iframe){ //dialog与tooltip不能有iframe
				if (opt.url) {
					w.jqp_bd.load(t._getUrl(opt.url), null, function(){  t.resetPlace(id); });
					t._startRefresh(id);
				}
				else {
					if (opt.id && jqp_id.length > 0){}
					else if (opt.content) w.jqp_bd.html(opt.content.replace('\n', '<br />'));
				}
			}
			else if (opt.url){
				try {
					if (!opt.width){
						opt.width = 320;
						w.jqp_panel.css({width: opt.width});
					}
					if (!opt.height){
						opt.height = 240;
						w.jqp_bd.css({height: opt.height - w.hd_height - w.ft_height});
					}
					w.jqp_bd.css({padding: 1});
					w.jqp_bd.append('<iframe id="'+id+'_ifr" name="'+id+'_ifr" frameBorder="0" style="border:0px;width:100%;height:100%"></iframe>');
					w.jqp_iframe = $("#"+id+ '_ifr');
					w.jqp_iframe.attr("src",t._getUrl(opt.url));
					w.jqp_iframe.load(function(){  $("#"+id+ '_load').remove(); });
					t._startRefresh(id);
				} catch(e){}
			}

			if (opt.hide)w.hide();
			t._setPanelPlace(id);
			//w.jqp_panel.fadeIn();

			if (w.jqp_hd && opt.moveable) w.jqp_panel.jqDrag(w.jqp_hd, true, function(pos){ t.moveBy(pos.left, pos.top, id);t.focus(id);});
			if (w.jqp_resize) w.jqp_panel.jqResize(w.jqp_resize, true, function(pos){ t.resizeBy(pos.width, pos.height, id);t.focus(id);});

			w.jqp_panel.click(clf = function(e) {
				var o = e.target;

				t.focus(id);

				switch (o.className) {
					case 'jqp_close':
						t.close(id);
						break;

					case 'jqp_refresh':
						t.refresh(id);
						break;

					case 'jqp_submit':
					case 'jqp_cancel':
						var cb = opt.callback;
						if (cb)cb(o.className == 'jqp_submit', w);
						else if (opt.type == 'dialog' && o.className == 'jqp_submit'){
							var jForm = $("#"+id + '_bd form');
							if (jForm.length > 0){
								jForm.submit(function() {
									$(this).ajaxSubmit(opt.ajaxOptions);
									return false;
								});

								jForm.submit();
							}
						}
						t.close(id);
						break;

					case 'jqp_minimiz_open':
					case 'jqp_minimiz_close':
						t.minimiz(id);
						break;
				}
				return false; //返回false 阻止起泡
			});

			if (w.jqp_iframe) w.jqp_iframe.focus(function() { t.focus(id); });

			//if (opt.mask) w.jqp_mask.show();

			t.focus(id);

			$("#"+id + '_ok').focus();

			return w;
		},

		focus : function(id) {
			var t = this, w;
			if (!(w = t.getPanel(id)))return false;

			t.currId = w.id;

			if (w.jqp_mask)w.jqp_mask.css('z-index', t.zIndex++);
			w.jqp_panel.css('z-index', t.zIndex++);
			w.jqp_wrapper.css('z-index', t.zIndex++);
			w.zIndex = t.zIndex;

			//id = id + '_wrapper';
			//if (t.lastId)$("#"+t.lastId).removeClass('mceFocus');
			//t.lastId = id + '_wrapper';
			//$("#"+t.lastId).addClass('mceFocus');
		},

		close : function(id) {
			var t = this, w, d = window.document, ix = 0, fw, id;

			if (!(w = t.getPanel(id)))return false;

			if (!w.autoId) return t.hide(w.id);

			w.jqp_panel.fadeOut("fast", function(){
				if (w.jqp_iframe){
					w.jqp_iframe.unbind();
					//w.jqp_iframe.attr('src', 'about:blank');
				}

				t._stopRefresh(id);

				w.jqp_panel.unbind();
				w.jqp_panel.remove();

				if (w.jqp_mask) w.jqp_mask.remove();
				delete t.windows[w.id];
				delete w;

				$.each (t.windows, function(k, v) {
					if (v.zIndex > ix && v.is_hide == false) {
						fw = v;
						ix = v.zIndex;
					}
				});

				if (fw) t.focus(fw.id);
			});
		},

		hide : function(id){
			var t = this, w, d = window.document, ix = 0, fw, id;

			if (!(w = t.getPanel(id)))return false;

			w.jqp_panel.fadeOut("fast", function(){
				if (w.jqp_mask) w.jqp_mask.hide();
				w.is_hide = true;
				t._stopRefresh(id);

				$.each (t.windows, function(k, v) {
					if (v.zIndex > ix && v.is_hide == false) {
						fw = v;
						ix = v.zIndex;
					}
				});

				if (fw) t.focus(fw.id);
			});
		},

		show : function(id){
			var t = this, w, d = window.document, ix = 0, fw, id;

			if (!(w = t.getPanel(id)))return false;

			t._startRefresh(id);
			if (w.is_hide == false) return t.focus(w.id);
			w.jqp_panel.show();
			t._setContextXY(id);
			if (w.jqp_mask) w.jqp_mask.show();
			w.is_hide = false;
			t.focus(w.id);

			return w;
		},

		refresh : function(id){
			var t = this, w, d = window.document, opt;

			if (!(w = t.getPanel(id)))return false;
			opt = w.options;
			if (w.is_min == true || !opt.url) return false;

			if (w.jqp_iframe)$.Dom.getIframeDocument(w.jqp_iframe.get(0).id).location.reload();
			else w.jqp_bd.load(t._getUrl(opt.url));

			return w;
		},

		_startRefresh : function(id){
			var t = this, w, opt;
			if (!(w = t.getPanel(id)))return false;
			opt = w.options;
			if (!isNaN(opt.refresh) && opt.refresh > 0 && opt.url) w.interval = setInterval(function(){t.refresh(id)}, opt.refresh*1000);
		},

		_stopRefresh : function(id){
			var t = this, w;
			if (!(w = t.getPanel(id)))return false;
			if (w.interval) clearInterval(w.interval);
			w.interval = null;
		},

		minimiz : function (id){
			var t = this, w, d = window.document

			if (!(w = t.getPanel(id)))return false;

			if (!w.jqp_minimiz) return w;

			if (w.jqp_minimiz.is(".jqp_minimiz_open")){
				w.jqp_minimiz.removeClass("jqp_minimiz_open");
				w.jqp_minimiz.addClass("jqp_minimiz_close");
				w.jqp_bd.hide();
				if (w.jqp_ft)w.jqp_ft.hide();
				w.is_min = true;
			}
			else {
				w.jqp_minimiz.removeClass("jqp_minimiz_close");
				w.jqp_minimiz.addClass("jqp_minimiz_open");
				w.jqp_bd.show();
				if (w.jqp_ft)w.jqp_ft.show();
				w.is_min = false;
			}

			return w;
		},

		alert : function(txt, cb) {
			var t = this, w;

			w = t.open({
				title : "Info",
				type : 'alert',
				mask : true,
				callback : cb,
				content : $.global.htmlEncode(txt),
				width : 400
			});
			return w;
		},

		confirm : function(txt, cb) {
			var t = this, w;

			w = t.open({
				title : "Confirm",
				type : 'confirm',
				mask : true,
				callback : cb,
				content : $.global.htmlEncode(txt),
				width : 400
			});
			return w;
		},

		tooltip : function(el, txt){
			var t = this, w, opt;
			el = $(el);

			opt = {type:'tooltip', resizeable:false, moveable:false, closeable:false, content:txt, id:$.global.generateId()};

			el.hover(
				function(e){ if (!w)w = t.open(opt); w.options.left = e.pageX+10; w.options.top = e.pageY; t._setContextXY(w.id); w.show(); },
				function(){ w.hide(); }
			);
		},

		panel : function(opt) {
			w = this.open(opt || {});
			return w;
		},

		dialog : function(opt, cb) {
			opt = opt || {};
			opt.type = 'dialog';
			opt.callback = cb;

			w = this.open(opt);
			return w;
		},

		dropdown : function(el, opt){
			var t = this, w, on_panel=false, on_el=false, tmo_el = null, tmo_panel = null;
			el = $(el);

			opt = $.extend(opt || {}, {type:'dropdown', resizeable:false, moveable:false, closeable:false});
			if (!opt.context)opt.context = [el, opt.align || 'bottom'];

			el.hover(
				function(e){
					if(!w){
						w = t.open(opt);
						w.jqp_panel.hover(
							function(){ w.show(); on_panel = true; if(tmo_el){ clearTimeout(tmo_el); tmo_el=null;}; if(tmo_panel){ clearTimeout(tmo_panel); tmo_panel=null;} },
							function(){ on_panel = false; tmo_panel = setTimeout(function (){if (!on_el)w.hide(); el.removeClass("nav_focus");},100); }
						);
					}
					w.show();
					el.addClass("nav_focus");
					on_el = true;
					if(tmo_el){ clearTimeout(tmo_el);
					tmo_el=null;};
					if(tmo_panel){ clearTimeout(tmo_panel); tmo_panel=null;}
				},
				function(){ on_el=false; tmo_el = setTimeout(function (){if (!on_panel)w.hide(); el.removeClass("nav_focus");},100);  }
			);
		},

		module : function (el, opt){
			var t = this, w;
			el = $(el);

			opt = opt || {};
			opt.el = el;

			opt.inline = true;
			w = t.open(opt);

			if (el.html()) el.click(function(e){ if (w.is_hide)w.show(); else w.hide(); return false; });

			return w;
		},

		moduleGroup : function(el, panels){
			var t = this, w;
			$(el).each(function(i) { t.groupAreas[i] = $(this); });
			$.each(panels, function(key, val){
				if (val.id) t.groupModules[val.id] = val;
				else if (typeof val == 'object' || typeof val == 'array'){
					$.each(val, function(k, v){
						if (v.id)t.groupModules[v.id] = v;
					})
				}
			});
		},

		resizeBy : function(dw, dh, id) {
			var t = this,  w, opt;
			if (!(w = t.getPanel(id)))return false;
			opt = w.options;

			if (w) {
				if (opt.inline) dw = "100%";
				w.jqp_panel.css("width", dw);
				w.jqp_bd.css("height", dh-w.hd_height-w.ft_height);
			}
		},

		moveBy : function(left, top, id) {
			var t = this,  w, pos={}, i, n, A, firstA, M, firstM, Apos={}, firstApos={}, Mpos={}, firstMpos={};
			if (!(w = t.getPanel(id)))return false;

			if (w) {
				if (t.groupAreas.length > 0 && t.groupModules[w.id]){
					for (var n in t.groupAreas){
						A = t.groupAreas[n];
						Apos = {x:A.offset().left, y:A.offset().top, w:A.width(), h:A.height()};
						if (left > Apos.x && top > Apos.y){
							if (!firstApos.x || (firstApos.x < Apos.x || firstApos.y < Apos.y)){ firstA = A; firstApos = Apos; };
						}
					}

					for (var i in t.groupModules){
						M = t.groupModules[i].jqp_panel;
						Mpos = {x:M.offset().left, y:M.offset().top, w:M.width(), h:M.height()};
						if (id != i && top > Mpos.y && (firstApos.x && Mpos.x > firstApos.x && Mpos.x < firstApos.x+firstApos.w && Mpos.y > firstApos.y && Mpos.y < firstApos.y+firstApos.h)){
							if (!firstMpos.x || (firstMpos.x < Mpos.x || firstMpos.y < Mpos.y)){ firstM = M, firstMpos = Mpos; }
						}
					}

					if (firstM)firstM.after(w.jqp_panel);
					else if (firstA) firstA.prepend(w.jqp_panel);
				}
				else w.jqp_panel.css({left:left, top:top});
			}
		},

		resetPlace : function(id){
			var t = this,  w,  opt;
			if (!(w = this.getPanel(id)))return false;
			opt = w.options;

			if (!opt.width) w.jqp_panel.css('width', '');
			if (!opt.height) w.jqp_bd.css('height', '');
			$("#"+id+ '_load').remove();
			t._setPanelPlace(id);
		},

		getPanel : function(id){
			var t = this, w;

			if (!id)id = t.currId;
			if (!id) return false;

			if (w = t.windows[id]) return w;
			return false;
		},

		_setPanelPlace : function(id){
			var t = this, w, opt, vp, panel_top, panel_left, bd_h, opt_width;

			if (!(w = this.getPanel(id)))return false;
			opt = w.options;

			opt_width = opt.width || w.jqp_panel.width();
			if (opt_width < opt.min_width)opt_width = opt.min_width;
			if (opt_width > opt.max_width)opt_width = opt.max_width;
			if (opt.inline) opt.width = "100%";
			w.jqp_panel.css("width", opt_width);

			if (!opt.height){
				bd_h = w.jqp_bd.height();
				if (bd_h < opt.min_height) bd_h = opt.min_height;
				if (bd_h > opt.max_height) bd_h = opt.max_height;
				w.jqp_bd.css("height", bd_h);
			}

			if (!this._setContextXY(id)) {
				vp = $.Dom.getViewPort();
				panel_left = opt.left || Math.round(Math.max(vp.x, vp.x + (vp.w / 2.0) - (w.jqp_panel.width() / 2.0)));
				panel_top = opt.top || Math.round(Math.max(vp.y, vp.y + (vp.h / 2.0) - (w.jqp_panel.height() / 2.0)));

				w.jqp_panel.css({top:panel_top, left:panel_left});
			}
		},

		_setContextXY : function(id){
			var w, opt, opt_xy={};

			if (!(w = this.getPanel(id)))return false;
			opt = w.options;

			if (opt.context && opt.context[0]) opt_xy = $.Dom.getContextXY(w.jqp_panel, opt.context[0], opt.context[1]);
			else if (opt.left && opt.top) opt_xy = $.Dom.getContextXY(w.jqp_panel, null, null, {x:opt.left, y:opt.top});

			if (opt_xy.x && opt_xy.y){
				w.jqp_panel.css({top:opt_xy.y, left:opt_xy.x});
				return true;
			}
			return false;
		},

		_getUrl : function(url){
			if (url)url += (url.indexOf('?') == -1 ? '?' : '&') + 'random=' + (new Date().getTime());
			return url;
		}

	};

	function getParams(obj, params)
	{
		var opt = {}, title, url, rel, rels;
		obj = $(obj);
		if (title = obj.attr("title")) opt.title = title;
		if (url = obj.attr("href")) opt.url = url;
		var rel = obj.attr("rel");
		if (rel){
			rels = rel.split(/&|&amp;/);
			$.each(rels, function(k,v){
				vs = v.split("=");
				if (!/^([\d\.+\-]+|false|null|undefined)$/i.test(vs[1])) vs[1] = '"'+vs[1]+'"';
				if (vs[0]) eval("opt."+vs[0]+"="+vs[1]);
			})
		}
		if (params) opt = $.extend(opt, params);
		return opt;
	}

	$.fn.extend({
		alert: function(cb) {
			return this.each(function() {
				var opt = getParams(this);
				$(this).click(function(){ $.jqPanel.alert(opt.title, cb || opt.callback); this.blur(); return false; });
			});
		},
		confirm: function(cb) {
			return this.each(function() {
				var opt = getParams(this);
				if (!cb && opt.url && /\.\w+(\?[^\?]+)?$/.test(opt.url))cb = function(s){if (s)location.href=opt.url;}
				$(this).click(function(){ $.jqPanel.confirm(opt.title, cb); this.blur(); return false; });
			});
		},
		tooltip: function() {
			return this.each(function() {
				var opt = getParams(this);
				$(this).attr("title", "");
				$.jqPanel.tooltip(this, opt.title);
			});
		},
		panel: function(id, params) {
			return this.each(function() {
				if (typeof id == 'object') params = id;
				else {
					params = params || {};
					params.id = id;
				}
				var opt = getParams(this, params);
				if (!opt.id) opt.id = $.global.generateId();
				$(this).click(function(){ $.jqPanel.panel(opt); this.blur(); return false; });
			});
		},
		dialog: function(params, cb) {
			return this.each(function() {
				var opt = getParams(this, params);
				if (!opt.id) opt.id = $.global.generateId();
				$(this).click(function(){ $.jqPanel.dialog(opt, cb); this.blur(); return false; });
			});
		},
		dropdown: function(id, params) {
			return this.each(function() {
				if (typeof id == 'object') params = id;
				else {
					params = params || {};
					params.id = id;
				}
				var opt = getParams(this, params);
				if (!opt.id) opt.id = $.global.generateId();
				$.jqPanel.dropdown(this, opt);
			});
		},
		module: function(params) {
			var ws = [];
			this.each(function(i) {
				var opt = getParams(this, params);
				if (!opt.id) opt.id = $.global.generateId();
				ws[i] = $.jqPanel.module(this, opt);
			});
			return ws;
		},
		moduleGroup: function() {
			$.jqPanel.moduleGroup(this, arguments);
		},
		closePanel: function() {
			return this.each(function() {
				var opt = getParams(this);
				$(this).click(function(){ $.jqPanel.close(opt.id); this.blur(); return false; });
			});
		}
	});

	var act = ['closePanel','closeDialog','closeAlert','closeConfirm'];

	$.alert = function (txt, cb){ return $.jqPanel.alert(txt, cb); };
	$.confirm = function (txt, cb){ return $.jqPanel.confirm(txt, cb); };
	$.tooltip = function (el, txt){ return $.jqPanel.tooltip(el, txt); };
	$.dialog = function (opt, cb){ return $.jqPanel.dialog(opt, cb); };
	$.panel = function (id, opt){ if (typeof id == 'object') opt = id; else { opt = opt || {}; opt.id = id; } return $.jqPanel.panel(opt); };
	$.dropdown = function (el, id, opt){ if (typeof id == 'object') opt = id; else { opt = opt || {}; opt.id = id; } return $.jqPanel.dropdown(el, opt); };
	$.module = function (el, opt){ return $.jqPanel.module(el, opt); };
	$.showPanel = function (id){ $.jqPanel.show(id); };
	$.closePanel = function (id){ $.jqPanel.close(id); };

})(jQuery);
